import router from '@Admin/router'
import useUserStore from '@Admin/utils/stores/modules/user'
import { PageEnum } from '@Admin/utils/enums/pageEnums'
// 免登录
const whiteList: string[] = [PageEnum.LOGIN, PageEnum.NOT_FOUND]
router.beforeEach(async (to, from, next) => {
    const userStore = useUserStore()

    if (whiteList.includes(to.path)) {
        // 在免登录白名单，直接进入
        next() 
    }else if (userStore.token) {
        // 判断是否已登录
        const hasGetUserInfo = Object.keys(userStore.userInfo).length !== 0
        if (hasGetUserInfo) {
             if (to.path === PageEnum.LOGIN) {
                next({ path: PageEnum.HOME })
             }else {
                next()
             }
        }else {
            await userStore.getUserInfo()
            next()
        }
    }else{
        next({ path: PageEnum.LOGIN  , query: { redirect: to.fullPath }})
    }
})

router.afterEach((to, from, next) => {
 
})

